package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.entity.QueryProjectInfo;
import com.todod.mapper.UsersManualFileMapper;
import com.todod.model.UsersManualFile;
import com.todod.service.IUsersManualFileService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gsh
 * @since 2020-11-23
 */
@Service
@Transactional
public class UsersManualFileServiceImpl extends ServiceImpl<UsersManualFileMapper, UsersManualFile>
		implements IUsersManualFileService {
	@Autowired
	private UsersManualFileMapper usersManualFileMapper;

	@Override
	public IPage<UsersManualFile> getListPage(Long _userId, QueryProjectInfo queryProjectInfo) {
		Page<UsersManualFile> page = new Page<>(queryProjectInfo.getPage(), queryProjectInfo.getLimit());

		IPage<UsersManualFile> iPage = usersManualFileMapper.getListPage(page, _userId, queryProjectInfo);

		return iPage;
	}

	@Override
	public void saveData(Long _userId, UsersManualFile usersManualFile) {

		usersManualFile.setId(SnowflakeIdGenerator.getId());
		usersManualFile.setCode(String.valueOf(SnowflakeIdGenerator.getId()));
		usersManualFile.setVersion(1);
		usersManualFile.setClassifyId(usersManualFile.getClassifyId());
		usersManualFile.setParentId(usersManualFile.getClassifyId());

		usersManualFile.setIsNew(1l);
//		设置删除标识为1（默认：为1）
		usersManualFile.setFlag(1);
		usersManualFile.setCreatetimeTi(LocalDateTime.now());
		usersManualFile.setCreateuserId(_userId);
		usersManualFileMapper.insert(usersManualFile);
	}

	@Override
	public void editData(Long _userId, UsersManualFile usersManualFile) {

		UsersManualFile usersManualFile1 = usersManualFileMapper.selectById(usersManualFile.getId());
		usersManualFile1.setIsNew(2l);

		usersManualFile1.setCreateuserId(usersManualFile1.getCreateuserId());
		usersManualFile1.setCreatetimeTi(usersManualFile1.getCreatetimeTi());
		usersManualFile1.setModifyuserId(_userId);
		usersManualFile1.setModifytimeTi(LocalDateTime.now());
		usersManualFileMapper.updateById(usersManualFile1);

		usersManualFile.setId(SnowflakeIdGenerator.getId());
		usersManualFile.setVersion(usersManualFile1.getVersion() + 1);
		usersManualFile.setCode(usersManualFile1.getCode());
//		设置删除标识为1（默认：为1）
		usersManualFile.setFlag(1);
		usersManualFile.setIsNew(1l);

		usersManualFile.setCreateuserId(_userId);
		usersManualFile.setCreatetimeTi(LocalDateTime.now());
		usersManualFileMapper.insert(usersManualFile);
	}

	@Override
	public List<UsersManualFile> getListByCode(String code) {

		List<UsersManualFile> list = usersManualFileMapper.getListByCode(code);

		return list;
	}

	@Override
	public List<UsersManualFile> getInfo(String code, String isNew) {
		List<UsersManualFile> list = usersManualFileMapper.getInfo(code, isNew);

		return list;
	}

	@Override
	public List<UsersManualFile> getFileList(Long _userId, Long companyId) {
		List<UsersManualFile> list = usersManualFileMapper.getFileList(_userId, companyId);

		return list;
	}

	@Override
	public List<UsersManualFile> getListByXmId(String xmId) {
		List<UsersManualFile> list = usersManualFileMapper.getListByXmId(xmId);

		return list;
	}

	@Override
	public List<UsersManualFile> getListByClassifyId(Long classifyId) {
		QueryWrapper<UsersManualFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("umf_classify_id", classifyId);
		queryWrapper.eq("umf_is_new", 1L);
		queryWrapper.eq("umf_flag", 1L);
		queryWrapper.orderByDesc("umf_sort_num");

		List<UsersManualFile> list = usersManualFileMapper.selectList(queryWrapper);

		return list;
	}

	@Override
	public List<UsersManualFile> getListByXftId(Long code) {
		QueryWrapper<UsersManualFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("umf_classify_id", code);
		queryWrapper.orderByDesc("umf_sort_num");

		List<UsersManualFile> list = usersManualFileMapper.selectList(queryWrapper);

		return list;
	}

}
